<?php

$oReq = &App::getRequest();
$oTpl = &App::getTemplate();
$oDb = &Database::get();
$oLang = &App::getLanguage();

$aSupported = array('php');

if ( $oReq->get('submit') )
{
	if ( isset($_FILES['package']['name']) )
	{
		$aError = array();

		if ( !in_array(substr($_FILES['package']['name'], -3), $aSupported) )
		{
			$aError[] = App::format('language_supported', "." . implode(", .", $aSupported));
		}

		if ( count($aError) == 0 )
		{
			$sTempName = "temp". time() .".php";

			@move_uploaded_file($_FILES['package']['tmp_name'], cachedDir . $sTempName);

			if ( !file_exists(cachedDir . $sTempName) )
			{
				$aError[] = App::format('language_error');
			}
			else
			{
				
				@chmod(cachedDir . $sTempName, 0644);
				
				if (!$oReq->get('delay'))
				{
					$sTempFile = file_get_contents(cachedDir . $sTempName);
					@eval($sTempFile);				
				}
				else 
				{
					$sTempFile = file(cachedDir . $sTempName);
					
					$iLine = 0;
					$iContent = 0;
					$sContent = '';
					foreach ($sTempFile as $sLine) 
					{			
						$iLine++;
						if ((
							$iLine == 250
							|| $iLine == 500 
							|| $iLine == 750 
							|| $iLine == 1000
							|| $iLine == 1250
							|| $iLine == 1500
							|| $iLine == 1750
							|| $iLine == 2000
							|| $iLine == 2250
							|| $iLine == 2500
							|| $iLine == 2750
							|| $iLine == 3000
							|| $iLine == 3250
							|| $iLine == 3500
							|| $iLine == 3750
							|| $iLine == 4000) && (substr(trim($sContent), -2) == ');')
						)
						{				
							$iContent++;
							eval($sContent);
							$sContent = '';
							$sLine = '';
							sleep(10);
						}
						$sContent .= $sLine;
					}
				
					if ($iContent)
					{
						eval($sContent);
					}
				}
				
				$oDb->query("OPTIMIZE TABLE `". App::getT('language_options') ."`");
				$oDb->query("OPTIMIZE TABLE `". App::getT('language_phrase') ."`");
				$oDb->update(App::getT('language'), array('default' => ''), "languageid = '". $iCachedId ."'");

				@unlink(cachedDir . $sTempName);
				
				App::rmCache('language_listing.php');		
			
				App::gotoUrl('admin.language.add', array(), App::format('language_added'), false);
			}
		}
	}
}

$oTpl->assign(array
(
	'aErr' => (isset($aError) ? $aError : ''),
));

?>